package com.ztercelstudio.demo002.Person

import android.util.Log

interface Action { // 接口默认是open的, 接口内方法可以实现，子类可以重定义， 方法默认也是open
    fun play() {
        Log.d("Demo002", "Action::play")
    }
    fun interest();
}

open class Person(var name: String, var age: Int): Action {  // 类加了open 才能被继承， 默认为final
    open fun print() {  // 方法加了open 才能被重定义
        Log.d("Demo002", "Person - $name age: $age")
    }

    override fun interest() {
        Log.d("Demo002", "Person::interest - like eat")
    }
}

class Student(name: String, age: Int, var gradeName: Int, var className: Int): Person(name, age) {
    override fun play() {
        Log.d("Demo002", "Student::play - play phone");
    }

    override fun print() {
        super.print()

        Log.d("Demo002", "Student - $name age: $age, grandName: $gradeName, className: $className")
    }
}

//----------------------------------------------------------
interface IInterface {
    var data: Int

    fun action()
    fun testFromIInterfacec() {
        Log.d("Demo002", "IInterface::testFromIInterfacec $data");
    }
}

interface IOperator {
    fun operate()
}

abstract class Base(var name: String = "Base") {
    abstract fun method();
    fun methodImpl() {
        Log.d("Demo002", "Base::methodImpl - value: $name");
    }
}

class Sub1: Base("Sub1"), IInterface, IOperator {  // 接口可继承有多个，抽象类只能一个
    override var data: Int = 100

    override fun method() {
        Log.d("Demo002", "Sub1::method - data: $data name: $name");
    }

    override fun action() {
        Log.d("Demo002", "Sub1::action - data: $data  name: $name");
    }

    override fun operate() {
        Log.d("Demo002", "Sub1::operate - data: $data name: $name");
    }
}

class Sub2: Base("Sub2"), IInterface, IOperator {
    override var data: Int = 999

    override fun method() {
        Log.d("Demo002", "Sub2::method - data: $data name: $name");
    }

    override fun action() {
        Log.d("Demo002", "Sub2::action - data: $data name: $name");
    }

    override fun operate() {
        Log.d("Demo002", "Sub2::operate - data: $data name: $name");
    }
}

fun getObject(obj: Base) {
    obj.method();
}

fun classInherit() {
    var person = Person("yiguigen", 22);
    person.name = "YiGuiGen"
    person.print()
    person.interest()
    person.play()

    var student = Student("zTercel", 32, 1, 13);
    student.print()
    student.interest();
    student.play();

    Log.d("Demo002", "-------- 接口 & 抽象类 -----------")
    var sub1 = Sub1()
    sub1.action()
    sub1.operate()
    sub1.testFromIInterfacec();

    var sub2 = Sub2()
    sub2.action()
    sub2.operate()
    sub2.testFromIInterfacec();

    Log.d("Demo002", "-------- 类多态 -----------")
    getObject(Sub1())
    getObject(Sub2())

}
